package com.shangcheng.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shangcheng.dubbo.api.SmsHomeRecommendSubjectApi;
import com.shangcheng.model.domain.SmsHomeNewProduct;
import com.shangcheng.model.domain.SmsHomeRecommendProduct;
import com.shangcheng.model.domain.SmsHomeRecommendSubject;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.server.exception.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/13 14:15
 */
@Service
public class SmsHomeRecommendSubjectService {
    @DubboReference
    private SmsHomeRecommendSubjectApi smsHomeRecommendSubjectApi;
    /**
     * 批量添加推荐专题
     * @param homeRecommendSubjectList
     */
    public void createSubject(List<SmsHomeRecommendSubject> homeRecommendSubjectList) {
        //根据品牌名称查询数据库
        List<SmsHomeRecommendSubject> homeRecommendSubjects= smsHomeRecommendSubjectApi.findByName(homeRecommendSubjectList);
        if (!ObjectUtils.isEmpty(homeRecommendSubjects)){
            //如果存在,抛出异常
            List<String> subjectNames = CollUtil.getFieldValues(homeRecommendSubjects, "subjectName", String.class);
            throw new BusinessException(subjectNames.toString()+"已存在");
        }
        smsHomeRecommendSubjectApi.createSubject(homeRecommendSubjectList);
    }

    /**
     * 批量删除推荐专题
     * @param ids
     */
    public void deleteSubject(Integer[] ids) {
        smsHomeRecommendSubjectApi.deleteSubject(ids);
    }

    /**
     * 分页查询推荐专题
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     */
    public PageResult SubjectList(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        IPage<SmsHomeRecommendSubject> page = smsHomeRecommendSubjectApi.subjectList(pageNum, pageSize, subjectName, recommendStatus);
        return new PageResult(pageNum,pageSize, Convert.toInt(page.getTotal()),page.getRecords());
    }

    /**
     * 批量修改推荐状态
     * @param ids
     * @param recommendStatus
     */
    public void updateSubjectStatus(Integer[] ids, Integer recommendStatus) {
        smsHomeRecommendSubjectApi.updateSubjectStatus(ids,recommendStatus);
    }

    /**
     * 修改推荐排序
     * @param id
     * @param sort
     */
    public void updateSubjectSort(Integer id, Integer sort) {
        smsHomeRecommendSubjectApi.updateSubjectSort(id,sort);
    }
}
